#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dp[maxn];
bool flag[maxn];
char str[maxn];
bool pre(LL num,LL len){
     char p[20];LL tot=0;
     for(LL i=1;i<=len;i++){
         if((1<<(i-1))&num){
            p[++tot]=str[i];
         }
     }
     for(LL i=1;i<=tot;i++){
         if(p[i]!=p[tot-i+1]){
            return 0;
         }
     }
     return 1;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL t;cin>>t;
   while(t--){
       memset(dp,0x3f,sizeof(dp));
       memset(flag,0,sizeof(flag));
       cin>>(str+1);LL n=strlen(str+1);
       for(LL i=0;i<(1<<n);i++){
           if(pre(i,n) ) flag[i]=true;
       }
       dp[0]=0;
       for(LL x=0;x<(1<<n);x++){
          for(LL j=x; j ;j=(j-1)&x){
              if(flag[j])
              dp[x]=min(dp[x],dp[j^x]+1);
          }
       }
       cout<<dp[(1<<n)-1]<<"\n";
   }
   return 0;
}
